<?php
defined('IN_IXDCW') or exit('Access Denied');
$do = new OwnCardModule();
$owncardAction = Db('owncard');
$menus = array (
    array('车主信息', '?moduleid='.$moduleid),
    array('订购信息', '?moduleid='.$moduleid.'&action=buy'),
);

if($_catids) require DT_ROOT.'/admin/admin_check.inc.php';

if($action == '') {
	$sfields = array('会员名', '卡号');
	$dfields = array( 'username', 'cardnum');
	$sorder  = array('结果排序方式', '认证时间降序', '认证时间升序');
	$dorder  = array($MOD['order'], 'addtime DESC', 'addtime ASC');

	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;

	isset($datetype) && in_array($datetype, array('edittime', 'addtime')) or $datetype = 'addtime';

	$fromtime = $fromdate ? strtotime($fromdate.' 0:0:0') : 0;
	$totime = $todate ? strtotime($todate.' 23:59:59') : 0;


	$fields_select = dselect($sfields, 'fields', '', $fields);
	$order_select  = dselect($sorder, 'order', '', $order);
	$condition = 'status=1';
	if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	if($psize) $do->setPagesize($psize);
	if($fromtime) $condition .= " AND `$datetype`>=$fromtime";
	if($totime) $condition .= " AND `$datetype`<=$totime";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
}elseif($action == 'buy'){
	$sfields = array('会员名', '联系人');
	$dfields = array( 'username', 'truename');
	$sorder  = array( '订购时间降序', '订购时间升序','发货时间降序', '发货时间升序');
	$dorder  = array( 'buytime DESC', 'buytime ASC','sendtime DESC', 'sendtime ASC');
	$sstatus = array('订购状态','未付款', '已付款','已发货');
	$dstatus  = array( '','1', '2','3');

	isset($datetype) && in_array($datetype, array('buytime', 'sendtime')) or $datetype = 'buytime';
	isset($fields) && isset($dfields[$fields]) or $fields = 0;
	isset($order) && isset($dorder[$order]) or $order = 0;
	isset($status) && isset($dorder[$status]) or $order = 0;
	$status = isset($status) ? intval($status) : 0;


	$fromtime = $fromdate ? strtotime($fromdate.' 0:0:0') : 0;
	$totime = $todate ? strtotime($todate.' 23:59:59') : 0;

	$areaid = isset($areaid) ? intval($areaid) : 0;
	$fields_select = dselect($sfields, 'fields', '', $fields);
	$order_select  = dselect($sorder, 'order', '', $order);
	$status_select  = dselect($sstatus, 'status', '', $status);

	$condition = 'status != 0';
	if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
	if($psize) $do->setPagesize($psize);
	if($areaid) $condition .= ($AREA[$areaid]['child']) ? " AND areaid IN (".$AREA[$areaid]['arrchildid'].")" : " AND areaid=$areaid";
	if($status) $condition .= " AND `status`=$status";
	if($fromtime) $condition .= " AND `$datetype`>=$fromtime";
	if($totime) $condition .= " AND `$datetype`<=$totime";
	$timetype = strpos($dorder[$order], 'edit') === false ? 'add' : '';
}
switch($action) {
	case 'buy':
			$lists = $do->getbuyList($condition, $dorder[$order]);
			$menuid = 1;
			include tpl('buy', $module);
	break;
	case 'send':
		$itemid or msg();
		$r = $owncardAction->getByitem($itemid);
		$email = trim($r['email']);
		$title = $MOD['sendalert_title'];
		$content =  str_replace('username',$r['truename'],$MOD['sendalert']);
		$content =  str_replace('buynum',$r['buynum'],$content);
		send_mail($email, $title , $content);
		send_message($r['username'],$title,$content);
		$post = array();
		$post['itemid'] = $itemid;
		$post['status'] = 3;
		$post['sendtime'] =$DT_TIME;;
		if($owncardAction->updateBuycard($post)){
			Prompt::tip('操作成功', $forward);
		}
		break;
	case 'showcard':
		$r = $owncardAction->getByUsername($username);
		extract($r);
		include tpl('showcard', $module);
		break;
	case 'buyshownote':
		$r = $owncardAction->getByitem($itemid);;
		extract($r);
		include tpl('buyshownote', $module);
		break;
	case 'delmember':
		$uname or msg('请选择车主');
		$do->delmember($uname);
		dmsg('删除成功', $forward);
		break;
	case 'delete':
		$itemid or msg('请选择订购信息');
		$do->delete($itemid);
		dmsg('删除成功', $forward);
		break;
	case 'export':
		if($all){
			$lists = $do->getbuyList($condition, $dorder[$order]);
		}else{
			$itemid or msg('请选择客户信息');
			if(is_array($itemid)){
				$itemid = implode(',',$itemid);
				$lists = $db->getAll("select * from {$db->getPre()}owncard_buy where itemid in (".$itemid.")");
			}
		}
		$set['author'] = 'ixdcw';
		$set['title']  = '车主金卡订购信息';
		$set['intru'] = '车主金卡订购信息';
		$set['category'] = '车主金卡订购信息';
		$set['stitle'] = '车主金卡订购信息';

		$firstline['A'] = 'itemid|编号|10';
		$firstline['B'] = 'truename|联系人|20';
		$firstline['C'] = 'telephone|电话';
		$firstline['D'] = 'mobile|手机';
		$firstline['E'] = 'area|所在地区|100';
		$firstline['E'] = 'address|联系地址|50';
		$firstline['F'] = 'email|电子邮件|30';
		$firstline['G'] = 'buytime|订购时间';
		$firstline['H'] = 'sendtime|发货时间';
		$firstline['I'] = 'username|订购会员|32';
		$firstline['J'] = 'status|状态';
		$firstline['K'] = 'buynum|数量|10';
		$firstline['L'] = 'money|付款金额';
		$firstline['M'] = 'note|附言|30';
		foreach($lists as $k=>$t){
			$lists[$k]['buytime'] = date('Y-m-d H:i:s',$t['buytime']);
			$lists[$k]['sendtime'] = date('Y-m-d H:i:s',$t['sendtime']);
			$lists[$k]['note']      = str::stripSlashes($t['note']);
			$lists[$k]['money']      = '￥'.$t['money'];
			$lists[$k]['area']      = area_pos($t['areaid'],' ','','3421');
			if(!$t['sendtime']) $lists[$k]['sendtime']  = '未发货';
			if($t['status'] ==1 )$lists[$k]['status'] = '未付款';
			if($t['status'] ==2 )$lists[$k]['status'] = '已付款';
			if($t['status'] ==3 )$lists[$k]['status'] = '已发货';
		}
		$time = date('Y-m-d',$DT_TIME);
		$filename = '车主金卡_订购信息_'.$time.'.xls';
		Str::exportExcel($lists,$set,$firstline,$filename);
		break;
	default:
		$lists = $do->getList($condition, $dorder[$order]);
		$menuid = 0;
		include tpl('index', $module);
	break;
}
?>